package com.uvnos4j.matilda.app.controller;

import com.uvnos4j.matilda.commons.utils.Const;
import com.uvnos4j.matilda.commons.utils.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录 Controller
 * <p>
 * Description:处理登录相关处理的 Controller
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @since 2020-03-27 23:10:47
 */
@Controller
public class LoginController {

    /**
     * @return 登录页
     */
    @GetMapping(Const.AUTH_LOGIN_URL)
    public String login() {
        return "login";
    }

    /**
     * @return 首页
     */
    @GetMapping({"", Const.AUTH_DEFAULT_SUCCESS_URL})
    public String index() {
        return "index";
    }

    /**
     * @return 登录失败跳转
     */
    @PostMapping(value = "/login", params = {"error"})
    public String loginFailed(HttpServletRequest request, RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("errorMsg", request.getParameter("error"));
        // 重定向到 GET 方式的登录页
        return ResponseUtil.redirect(Const.AUTH_LOGIN_URL);
    }

    /**
     * @return 403
     */
    @GetMapping({Const.AUTH_ACCESS_DENIED_URL})
    public String error403() {
        return "error/403";
    }

    /**
     * @return 500
     */
    @GetMapping("/error500")
    public String error500() {
        return "error/500";
    }

}